package com.thingcom.delinrobot.main.Launch;

import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.Window;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.thingcom.delinrobot.Common.Constant;
import com.thingcom.delinrobot.R;
import com.thingcom.delinrobot.base.BaseActivity;
import com.thingcom.delinrobot.main.adddevice.DeviceListActivity;
import com.thingcom.delinrobot.utils.MyLog;
import com.thingcom.delinrobot.utils.PreferencesUtil;
import com.thingcom.delinrobot.utils.ToastUtil;

import pub.devrel.easypermissions.AfterPermissionGranted;
import pub.devrel.easypermissions.EasyPermissions;

import static android.Manifest.permission.ACCESS_COARSE_LOCATION;
import static android.Manifest.permission.ACCESS_FINE_LOCATION;
import static com.thingcom.delinrobot.Common.Constant.MAIL_S;

/**
 * author : YuFeng
 * e-mail : 923462992@qq.com
 * date   : 2019/11/27
 */
public class LaunchActivity extends BaseActivity {
    private String userMail;
    private String userPsw;
    public static final int REQUEST_CODE = 0x777;
    private static String[] permissions = new String[]{
            ACCESS_COARSE_LOCATION,
            ACCESS_FINE_LOCATION,
    };


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (!isTaskRoot()) {
            finish();
            return;
        }
        setContentView(R.layout.activity_login);
        userMail = PreferencesUtil.getInstance(this).getString(Constant.MAIL_NUMBER,null);
        userPsw = PreferencesUtil.getInstance(this).getString(MAIL_S,null);

        if (findFragment(WelcomeMainFragment.class) == null) {
            loadRootFragment(R.id.fl_container, LaunchFragment.newInstance());
            MyLog.i("权限", "loadRootFragment: ");
        }

        MyLog.i("权限", "SDK_INT: "+Build.VERSION.SDK_INT);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            checkPermission();
        } else {
            Handler handler1 = new Handler();
            handler1.postDelayed(() -> {

                if (userMail!=null&userPsw!=null){
                    Intent intent = new Intent(getApplicationContext(), DeviceListActivity.class);
                    startActivity(intent);
                    MyLog.i("登陆", "initView: "+" userMail:"+userMail+" userPsw:"+userPsw);
                } else {
                    start(WelcomeMainFragment.newInstance());
                }
            },1000);
        }

    }




    @AfterPermissionGranted(REQUEST_CODE)
    private void checkPermission(){
        MyLog.i("权限", "开始checkPermission: ");
        if (!EasyPermissions.hasPermissions(this,permissions)){
            EasyPermissions.requestPermissions(this,"欢迎使用,我们需要以下权限",REQUEST_CODE,permissions);
        }else {
            MyLog.i("权限", "权限已经全部启动: ");
            MyLog.i("权限", "(WelcomeMainFragment.newInstance()) ");
            //  ToastUtil.showToast(getApplicationContext(),"权限已经全部启动");
//            start(WelcomeMainFragment.newInstance());
            Handler handler1 = new Handler();
            handler1.postDelayed(() -> {

                if (userMail!=null&userPsw!=null){
                    Intent intent = new Intent(getApplicationContext(), DeviceListActivity.class);
                    startActivity(intent);
                    MyLog.i("登陆", "initView: "+" userMail:"+userMail+" userPsw:"+userPsw);
                } else {
                    start(WelcomeMainFragment.newInstance());
                }
            },1000);

        }
    }


    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        StringBuilder sb = new StringBuilder();
        boolean hasDeniedPermission = false;
        for (int i = 0; i < grantResults.length; i++) {
            //获取Denied,有否认的权限就添加
            if (grantResults[i] == PackageManager.PERMISSION_DENIED) {
                sb.append(permissions[i]);
                sb.append("\n");
                hasDeniedPermission = true;
            }
        }

        if (hasDeniedPermission) {
            //如果有取消的权限，你怎么处理呢？可以定义dialog提示用户
            checkPermission();
        }
        EasyPermissions.onRequestPermissionsResult(REQUEST_CODE, permissions, grantResults, this);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {

        super.onActivityResult(requestCode, resultCode, data);
       // MyLog.i(TAG, "requestCode: " + requestCode + "---resultCode: " + resultCode);

        checkPermission();

    }
}
